#ifndef CAMERA_H
#define CAMERA_H

//Adjusts the camera based on mouse movement
//Also forces the mouse to not go too far.
void Update_Camera_Position(float camera_speed, float cursor_distance) //Moves the camera based on the cursor's positino with the player
{
 float x_offset = (Scene2_CursorX - Camera_X) + (CharacterA->X_Position - Scene2_CursorX)/cursor_distance;
 float y_offset = (Scene2_CursorY - Camera_Y) + (CharacterA->Y_Position - Scene2_CursorY)/cursor_distance;
 Camera_X = Camera_X + x_offset/camera_speed;
 Camera_Y = Camera_Y + y_offset/camera_speed;
}

void Update_Cursor_Position(float x_distance, float y_distance) //Checks cursor to make sure it cannot move beyond the screen
{
 float x_offset = abs(Scene2_CursorX - CharacterA->X_Position);
 float y_offset = abs(Scene2_CursorY - CharacterA->Y_Position);
 if(x_offset < x_distance && y_offset < y_distance)
 {
  Scene2_PrevCurX = Scene2_CursorX; Scene2_PrevCurY = Scene2_CursorY; //Success, Saves the pre-edge position incase it needs to be restored.
  Scene2_CursorX = Scene2_CursorX + MousePosX - MousePrevX;
  Scene2_CursorY = Scene2_CursorY + MousePosY - MousePrevY;
  Scene2_CursorX = Scene2_CursorX + CharacterA->X_Velocity*Delta_Time - (.5*CharacterA->X_Acceleration*Delta_Time*Delta_Time);
  Scene2_CursorY = Scene2_CursorY + CharacterA->Y_Velocity*Delta_Time - (.5*CharacterA->Y_Acceleration*Delta_Time*Delta_Time);      
  //Double check to make sure it's okay.
  x_offset = abs(Scene2_CursorX - CharacterA->X_Position);
  y_offset = abs(Scene2_CursorY - CharacterA->Y_Position);
  if(x_offset >= x_distance && y_offset >= y_distance) //Double Check to make sure everything is good.
   {
    //restore previous okay position
    Scene2_CursorX = Scene2_PrevCurX; 
    Scene2_CursorY = Scene2_PrevCurY; 
   }
 }
 else
 {
  //restore previous okay position
  Scene2_CursorX = Scene2_PrevCurX; 
  Scene2_CursorY = Scene2_PrevCurY;       
 }
}

//Adjusts the camera based on player leaving screen. 
//Also, the border_distance value should be allowed to shift to implement "sniper mode" where it IS possible to see further away.
//For border distance, start with 0 as the camera having no free zone -> further.
//Border_distance is also directly relative to y distance. 50px = 50px up/down
void Adjust_Player_Camera(Player* Cur_Player, float border_distance, float delta_time, short up_down, short left_right) 
{
 //First do a check to see if the player is out of the inner box of the current camera position
 float screen_ratio = static_cast<float>(Screen_Width)/Screen_Height;
 float AbsX_Distance = screen_ratio*border_distance;
 float AbsY_Distance = border_distance;
 //Camera Character offset
 float X_Offset = Camera_X - Cur_Player->X_Position;
 float Y_Offset = Camera_Y - Cur_Player->Y_Position;
  //Then, if the box is outside of the bounds, get the player's current velocity and apply it to the camera via d = vt
  //Also, if he is almost out of the zone, check if he is pressing in the outward direction.
 if((abs(X_Offset) < AbsX_Distance) && (abs(Y_Offset) < AbsY_Distance)) return; //Safe!
 else
 {
  Camera_X = Camera_X + Cur_Player->X_Velocity*delta_time + .5*Cur_Player->X_Acceleration*delta_time*delta_time;
  Camera_Y = Camera_Y + Cur_Player->Y_Velocity*delta_time + .5*Cur_Player->Y_Acceleration*delta_time*delta_time;                     
 }
}

//LAWL Really stupid shaking. 
//Idea: Implement a rubber banding system that promotes the movement of camera to the opposite direction the further it goes to the other side.
void Do_Camera_Shake(float Intensity_X, float Intensity_Y)
{
 Camera_X = Camera_X + Intensity_X*sin(rand());
 Camera_Y = Camera_Y + Intensity_Y*sin(rand()); 
}

//Checks if the player is within range of the cursor
int CheckCursor(float soft_distance, float hard_distance, float screen_ratio)    
{
 //This should be a check against the cursor's world positon and the camera's world position. 
 float X_Offset = abs(Scene2_CursorX - Camera_X);
 float Y_Offset = abs(Scene2_CursorY - Camera_Y);
 if(X_Offset > hard_distance*screen_ratio) return 2; //Inside far end, do not permit cursor movement, do not move camera.
 if(Y_Offset > hard_distance)              return 2;
 if(X_Offset > soft_distance*screen_ratio) return 1; //Inside closer bounds, move camera and allow cursor movement.
 if(Y_Offset > soft_distance)              return 1;
 return 0; //Inside movable square, do not move camera, allow cursor movement.

}

#endif
